package com.x.crm.dao;

import com.x.crm.model.Channel;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ChannelDao extends BaseDao {

	private final static String selectChannelListAllSql = "SELECT * FROM CRM_CHANNEL T1 WHERE 1=1 ";

	/**
	 * 查询包括系统默认的数据
	 *
	 * @param Channel
	 * @return
	 * @throws Exception
	 */
	public List<Channel> selectChannelListAll(Channel channel) throws Exception {
		StringBuilder sql = new StringBuilder("SELECT * FROM (SELECT * FROM CRM_CHANNEL WHERE CHANNEL_ID < 100 ");
		sql.append("UNION ALL ");
		sql.append(selectChannelListAllSql);
		List<Object> params = new ArrayList<>();
		searchChannelCondition(channel,sql,params);
		sql.append(")T ORDER BY T.CHANNEL_CREATE_TIME DESC ");
		return this.queryForList(sql.toString(), Channel.class, params, channel);
	}

	/**
	 * 查询不包含系统默认的数据
	 * @param channel
	 * @return
	 * @throws Exception
	 */
	public List<Channel> selectChannelList(Channel channel) throws Exception {
		StringBuilder sql = new StringBuilder(selectChannelListAllSql);
		List<Object> params = new ArrayList<>();
		searchChannelCondition(channel,sql,params);
		sql.append("ORDER BY T1.CHANNEL_CREATE_TIME DESC ");
		return this.queryForList(sql.toString(), Channel.class, params, channel);
	}



	/**
	 * 查询包括系统默认的数据条数
	 *
	 * @param Channel
	 * @return
	 * @throws Exception
	 */
	public int selectChannelNum(Channel channel) throws Exception {
		StringBuilder sql = new StringBuilder("SELECT COUNT(T.CHANNEL_ID) FROM (SELECT * FROM CRM_CHANNEL WHERE CHANNEL_ID < 100 ");
		sql.append("UNION ALL ");
		sql.append(selectChannelListAllSql);
		List<Object> params = new ArrayList<Object>();
		searchChannelCondition(channel,sql,params);
		sql.append(")T ");
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询不包括系统默认的数据条数
	 *
	 * @param Channel
	 * @return
	 * @throws Exception
	 */

	private final static String selectChannelListNum = "SELECT COUNT(T1.CHANNEL_ID) FROM CRM_CHANNEL T1 WHERE 1=1 ";

	public int selectChannelListNum(Channel channel) throws Exception {
		StringBuilder sql = new StringBuilder(selectChannelListNum);
		List<Object> params = new ArrayList<Object>();
		searchChannelCondition(channel,sql,params);
		return this.queryForInt(sql.toString(), params);
	}


	public void  searchChannelCondition(Channel channel, StringBuilder sb, List<Object> params){
//		if (this.isNotNull(channel.getMarketId())) {
//			sb.append("AND T1.MARKET_ID = ? ");
//			params.add(channel.getMarketId());
//		}
		if (this.isNotNull(channel.getChannelState())) {
			sb.append("AND T1.CHANNEL_STATE = ? ");
			params.add(channel.getChannelState());
		}
		if (this.isNotNull(channel.getChannelName())) {
			sb.append("AND T1.CHANNEL_NAME = ? ");
			params.add(channel.getChannelName());
		}
		if (this.isNotNull(channel.getChannelId())) {
			sb.append("AND T1.CHANNEL_ID  =  ? ");
			params.add(channel.getChannelId());
		}
//		if (this.isNotNull(channel.getShopId())) {
//			sb.append("AND T1.SHOP_ID  = ? ");
//			params.add(channel.getShopId());
//		}
	}



}
